package com.yunxin.core.infra.persistence.repository.page;

import com.yunxin.core.domain.model.page.Page;
import com.yunxin.core.domain.model.page.PageRepository;
import com.yunxin.core.infra.persistence.converter.PageConverter;
import com.yunxin.core.infra.persistence.entity.page.PageDO;
import com.yunxin.core.infra.persistence.mapper.page.PageExtendMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

/**
 * @author zero°
 * @since 2023-07-13
 */
@Repository
@RequiredArgsConstructor
public class PageRepositoryImpl implements PageRepository {

    private final PageExtendMapper pageExtendMapper;

    private final PageConverter pageConverter;

    @Override
    public boolean savePage(Page page) {
        PageDO pageDO = pageConverter.formPage(page);
        int count = pageExtendMapper.insertSelective(pageDO);
        return count > 0;
    }

    @Override
    public boolean updatePage(Page page) {
        PageDO pageDO = pageConverter.formPage(page);
        int count = pageExtendMapper.updateByPrimaryKeySelective(pageDO);
        return count > 0;
    }

    @Override
    public Page getPageById(Long id) {
        PageDO pageDO = pageExtendMapper.selectByPrimaryKey(id);
        return pageConverter.toPage(pageDO);
    }
}
